const access = require('./access')
const socket = require('socket.io')
function fnSocket(server){
  fnSocket.io = socket(server)  
  fnSocket.io.on('connection', socket => {
    let userName;
    socket.on('message', msg => {
      if (userName && msg.content) {
        //全部 socket为个人
        fnSocket.io.emit('message', {
          id: socket.id,
          userName,
          content: msg.content,
          createAt: new Date(),
        });
      }
    });
    socket.on('entry', msg => {
      userName = msg.userName;
      fnSocket.access(socket, userName, 'entry')
    })
    socket.on('quit', msg => {
      fnSocket.access(socket, userName, 'quit')
    })
    socket.on('disconnect', msg => {
      fnSocket.access(socket, userName, 'quit')
    })
    // socket.send('终于等到你');
  })
}
fnSocket.access = access
fnSocket.users = new Set()//人数
module.exports = fnSocket

